package com.lin.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import com.lin.bean.CallHistoryBean;
import com.lin.fragments.PersonalDetailCardCallHistoryFragment;
import com.lin.fragments.PersonalDetailCardMSMAndCALLFragment;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.ChatActivity;
import com.yunfeng.supercall.activity.OutCallActivity;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.PhoneContactsBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.UserBean;
import com.lin.utils.Parameters;
import com.lin.utils.Session;
import com.lin.utils.Session.ResultReceiver;
import com.lin.utils.Session.SignupResult;
import com.lin.utils.Utils;
import com.lin.views.ListViewForPersonalCardHistoryCall;
import com.lin.views.ListViewForPersonalCardHistoryCall2;
import com.lin.views.ListViewForRecentCall;
import com.lin.views.MyFragmentAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.CallLog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PersonalDetailCardInPhoneActivity extends FragmentActivity implements Constants, OnClickListener{
	ImageView imageViewAvatar;
	private TextView mTextViewGroup;
	private PhoneContactsBean mSuperFriendsBean;
	ArrayList<PhoneContactsBean> mList;
	DisplayImageOptions options;
	private ListViewForPersonalCardHistoryCall2 mListView;
	private TextView mTextViewUserName;

	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	private ImageView imageViewSex;
	private FrameLayout mFrameLayoutInviteFriends3;
	private FrameLayout mFrameLayoutSendMessage1;
	private FrameLayout mFrameLayoutFocus2;
	private ImageView imageViewSendMsn;
	private ImageView imageViewCall;
	private FrameLayout mFrameLayoutPhone;
	private LinearLayout linearLayoutCallHistory;
	private TextView mTextViewPhone;
	Session mSession;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		options = new DisplayImageOptions.Builder()
		.displayer(new PinterestCircleBitmapDisplayer(getResources().getDimensionPixelSize(R.dimen.corners_avatar)))
		.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();

		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);
		
		
		setContentView(R.layout.activity_personal_in_phone_detailcard);
		imageViewSex = (ImageView) findViewById(R.id.imageview_sex);
		imageViewSendMsn = (ImageView) findViewById(R.id.imageview_send_msn);
		mListView = (ListViewForPersonalCardHistoryCall2) findViewById(R.id.listview);

		imageViewAvatar = (ImageView) findViewById(R.id.imageview_avatar);
		mTextViewGroup = (TextView) findViewById(R.id.textview_group);
		mTextViewUserName = (TextView) findViewById(R.id.textview_username);
		imageViewCall = (ImageView) findViewById(R.id.imageview_call);

		mSuperFriendsBean = getIntent().getParcelableExtra(KEY_DATA);
		String group = getIntent().getStringExtra("group");
		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);
		mFrameLayoutPhone = (FrameLayout) findViewById(R.id.framelayout);
		mFrameLayoutSendMessage1 = (FrameLayout) findViewById(R.id.framelayout_personal1);
		mFrameLayoutFocus2 = (FrameLayout) findViewById(R.id.framelayout_personal2);
		mFrameLayoutInviteFriends3 = (FrameLayout) findViewById(R.id.framelayout_personal3);
		linearLayoutCallHistory = (LinearLayout) findViewById(R.id.ll_call_history);
		mTextViewPhone = (TextView) findViewById(R.id.textview_phone2);
		mTextViewPhone.setText(mSuperFriendsBean.getPhone() + "");
		mFrameLayoutFocus2.setVisibility(View.GONE);
		mFrameLayoutInviteFriends3.setVisibility(View.GONE);
		
		imageViewCall.setOnClickListener(this);
		imageViewSendMsn.setOnClickListener(this);
		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);
		mFrameLayoutFocus2.setOnClickListener(this);
		mFrameLayoutInviteFriends3.setOnClickListener(this);
		mFrameLayoutSendMessage1.setOnClickListener(this);

		ImageLoader.getInstance().displayImage(mSuperFriendsBean.getPhoto(), imageViewAvatar, options);
		mTextViewGroup.setText(group);
		ArrayList<CallHistoryBean> mData = getCallHistory2();
		ArrayList<CallHistoryBean> mDataFiltered = new ArrayList<CallHistoryBean>(); 
		for(Iterator<CallHistoryBean> iterator = mData.iterator(); iterator.hasNext();) {
			CallHistoryBean mBean = iterator.next();
			try {
				if(mBean.getNumber().equals(mSuperFriendsBean.getPhone())) {
					mDataFiltered.add(mBean);
				}
			} catch(NullPointerException e) {

			}
		}
		mListView.setDataList(mDataFiltered);

		//			mTextViewAddress.setText(userBean.getAddress());
		//			mTextViewSign.setText(userBean.getNote());
		mTextViewUserName.setText(mSuperFriendsBean.getNick_name());
		mFrameLayoutPhone.setVisibility(View.VISIBLE);
	}
	//		mPhoneContactsBeanList = (ArrayList<PhoneContactsBean>) getIntent().getSerializableExtra(KEY_DATA);


	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(mFrameLayoutBack == view) {
			finish();
		} else if(mFrameLayoutMore == view) {
	/*		Intent intent = new Intent(this, ShareActivity.class);
			startActivity(intent);*/
//			int userid = BaseActivity.getUser(this).getUserid();
		} else if(mFrameLayoutFocus2 == view) {

		} else if(mFrameLayoutInviteFriends3 == view) {
		} else if(mFrameLayoutSendMessage1 == view ) {
			Utils.sendSMS(this, getResources().getString(R.string.sms_share, mSession.getmUserBean().getUserid(), mSuperFriendsBean.getPhone()));


		} else if(imageViewCall == view ) {
			Utils.dial(mSuperFriendsBean.getPhone(), PersonalDetailCardInPhoneActivity.this);
		} else if( imageViewSendMsn == view) {
			Utils.SendSMSTo(PersonalDetailCardInPhoneActivity.this, mSuperFriendsBean.getPhone());
		}




	}
	void getCallHistory () {
		String strNumber,strName = "";
		int type;
		long callTime;
		Date date;
		String time= "";
		ContentResolver cr = getContentResolver();
		final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,
				new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE, CallLog.Calls.DATE,CallLog.Calls.DURATION},
				null, null,CallLog.Calls.DEFAULT_SORT_ORDER);
		cursor.moveToPosition(0);
		strNumber = cursor.getString(0);    //呼叫号码
		strName = cursor.getString(1);   //联系人姓名
		type = cursor.getInt(2);  //来电:1,拨出:2,未接:3 public static final int INCOMING_TYPE = 1;   public static final int OUTGOING_TYPE = 2;   public static final int MISSED_TYPE = 3;
		long duration = cursor.getLong(4);
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		date = new Date(Long.parseLong(cursor.getString(3)));
		time = sfd.format(date);
		System.out.println("------"+duration);
		System.out.println("?????"+strNumber);
	}

	ArrayList<CallHistoryBean> getCallHistory2 () {
		ArrayList<CallHistoryBean> mList = new ArrayList<CallHistoryBean>();
		String strNumber,strName = "";
		Date date;
		String time= "";
		ContentResolver cr = getContentResolver();
		final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[] {
				CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME,
				CallLog.Calls.TYPE, CallLog.Calls.DATE, CallLog.Calls.DURATION }, null, null,
				CallLog.Calls.DEFAULT_SORT_ORDER);
		//				final Cursor cursor = cr.query(数据库表名，取得的数据数组（里边包含字段名称），条件，参数组，排序等信息） //这就相当于一条SQL语句//取得所有通话信息  这里边稍微有点复杂

		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
//			String number = cursor.getString(0);
			
			String number = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
			String name = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME));                 
			//通话时间,单位:s                                                                                      
//			String duration = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION));
			long duration = cursor.getLong(cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION));
			String callTime = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DATE)); 
			SimpleDateFormat sfd = new SimpleDateFormat("MM-dd hh:mm:ss");
			date = new Date(Long.parseLong(callTime));
			String strDate = sfd.format(date);
			int type = cursor.getInt(2);
			CallHistoryBean mCallHistoryBean = new CallHistoryBean(strDate , number, name, duration + "", type);
			mList.add(mCallHistoryBean);
			/*    if (type == 3) {
		    tel = str;*/
			//		    break;
		}
		return mList;
	} 
	//		System.out.println("?????"+strNumber);
}